package com.homson.servicedomain.domain.transaction;

import com.homson.coreservice.servicecommon.enumeration.TransCategoryEnum;
import com.homson.coreservice.servicecommon.expection.SystemException;
import com.homson.coreservice.servicecommon.vo.Operation;
import com.homson.coreservice.servicecommon.vo.TransactionVO;
import com.homson.servicedomain.domain.account.AccountQueryCommand;
import com.homson.coreservice.serviceinfrastructure.dto.AccountDto;
import com.homson.coreservice.serviceinfrastructure.dto.TransactionDto;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author: homson_leo
 * @contact: hey_超级巨星（Homson_Leo）- 刘泓岑 1196816884@qq.com
 * @time: 2025/11/1 4:21 PM
 **/
@Slf4j
@Component
public class TransactionRenderCommand {
    
    @Resource
    private TransactionQueryCommand transactionQueryCommand;
    @Resource
    private AccountQueryCommand accountQueryCommand;
    
    public List<TransactionVO> transactionListRender(Long userId, Integer pageNum, Integer pageSize) throws SystemException {
        try {
            List<TransactionDto> transactionDtos = transactionQueryCommand.queryRecentTransactionList(userId, pageNum, pageSize);
            List<Long> accountIds = transactionDtos.stream().map(TransactionDto::getAccountId)
                    .collect(Collectors.toList());
            
            List<AccountDto> accountDtoList = accountQueryCommand.findAccountByAccountIds(accountIds);
            
            Map<Long, AccountDto> accountTransMap = accountDtoList.stream()
                    .collect(Collectors.toMap(AccountDto::getAccountId, Function.identity()));
            
            List<Operation> operations = new ArrayList<>();
            operations.add(new Operation("撤销", 1, ""));
            
            List<TransactionVO> transactionVOs = parseTransactionVO(transactionDtos, accountTransMap, accountDtoList, operations);
            return transactionVOs;
            
        } catch (Exception e) {
            log.error(TransactionRenderCommand.class.getSimpleName(), "查询记账明细渲染异常", e);
            throw new SystemException(e.getMessage());
        }
    }
    
    private List<TransactionVO> parseTransactionVO(List<TransactionDto> transactionDtos, Map<Long, AccountDto> accountTransMap, List<AccountDto> accountDtoList, List<Operation> operations) {
        if (CollectionUtils.isEmpty(transactionDtos) || CollectionUtils.isEmpty(accountDtoList)) {
            return Collections.emptyList();
        }
        List<TransactionVO> transactionVOS = new ArrayList<>();
        for (TransactionDto each : transactionDtos) {
            TransactionVO transactionVO = new TransactionVO();
            Long accountId = each.getAccountId();
            AccountDto accountDto = accountTransMap.get(accountId);
            transactionVO.setTransTime(each.getTransDate());
            transactionVO.setAmount(each.getAmount());
            transactionVO.setAccountName(accountDto.getAccountName());
            transactionVO.setAssetName(each.getDescription());
            transactionVO.setType(TransCategoryEnum.of(accountDto.getAccountType(), each.getTransType()).getDescription());
            transactionVO.setOperationList(operations);
            transactionVOS.add(transactionVO);
        }
        return transactionVOS;
    }
    
}


